import 'package:extended_text_field/extended_text_field.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/page/ext/MessageSelectionControls.dart';
import 'package:im_app/im/util/ImConstant.dart';

import '../../../../pojo/ImPojo.dart';
import '../../../bloc/MessageListCubit.dart';
import '../../../bloc/MessageSendCubit.dart';
import '../../../ext/ImSpecialTextSpanBuilder.dart';

class TextFieldMessagePane extends StatefulWidget {
  final ImMessage _message;

  TextFieldMessagePane(this._message);

  @override
  State<StatefulWidget> createState() => _RichTextMessageSate();
}

class _RichTextMessageSate extends State<TextFieldMessagePane> {
  final FocusNode _focusNode = FocusNode();
  late TextEditingController _textEditingController;

  @override
  void initState() {
    super.initState();

    _textEditingController = TextEditingController();
    _textEditingController.value = TextEditingValue(text: widget._message.content??'');
    _focusNode.addListener(() => {if (!_focusNode.hasFocus) {

    }});
  }

  @override
  Widget build(BuildContext context) {
    // if (_message.messageType != ImMessageType.Text) {
    //   throw new Exception("消息类型不对" + _message.messageType.name);
    // }

    var messageSendCubit = context.read<MessageSendCubit>();
    var messageListCubit = context.read<MessageListCubit>();
    return Padding(
      padding: const EdgeInsets.only(top: 2, right: 5, bottom: 2, left: 5),
      child: ExtendedTextField(
        controller: _textEditingController,
        specialTextSpanBuilder: ImSpecialTextSpanBuilder(textStyle: const TextStyle(fontSize: 16)),
        decoration: const InputDecoration(
          border: InputBorder.none, // 移除边框/ 输入框提示文本
          contentPadding: EdgeInsets.zero,
          hintMaxLines: 100,
          isCollapsed: true,
          // filled: true,
        ),
        focusNode: _focusNode,
        readOnly: true,
        textAlign: TextAlign.left,
        expands: false,
        maxLines: null,
        style: const TextStyle(fontSize: ImConstant.IM_DEFAULT_SIZE),
        selectionControls: MessageSelectionControls(message: widget._message, messageSendCubit: messageSendCubit, messageListCubit: messageListCubit),
      ),
    );
  }

  List<Widget> _buildContextMenuButtons2(BuildContext context, EditableTextState editableTextState) {
    var results = <TextSelectionToolbarTextButton>[];
    results.add(TextSelectionToolbarTextButton(
      onPressed: () {
        editableTextState.copySelection(SelectionChangedCause.toolbar);
        ContextMenuController.removeAny();
      },
      padding: const EdgeInsets.only(left: 10, right: 10),
      child: Text('复制'),
    ));
    return results;
  }

  List<ContextMenuButtonItem> _buildContextMenuButtons(BuildContext context, EditableTextState editableTextState) {
    var results = <ContextMenuButtonItem>[];
    results.add(ContextMenuButtonItem(
      onPressed: () {
        editableTextState.copySelection(SelectionChangedCause.toolbar);
        ContextMenuController.removeAny();
      },
      label: '复制',
      type: ContextMenuButtonType.copy,
    ));

    results.add(ContextMenuButtonItem(
      onPressed: () {
        editableTextState.selectAll(SelectionChangedCause.toolbar);
      },
      label: '全选',
      type: ContextMenuButtonType.selectAll,
    ));
    results.add(ContextMenuButtonItem(
      onPressed: () {
        print("transfer!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        ContextMenuController.removeAny();
      },
      label: '转发',
      type: ContextMenuButtonType.custom,
    ));
    results.add(ContextMenuButtonItem(
      onPressed: () {
        print("collect!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        ContextMenuController.removeAny();
      },
      label: '收藏',
      type: ContextMenuButtonType.custom,
    ));

    results.add(ContextMenuButtonItem(
      onPressed: () {
        print("collect!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        ContextMenuController.removeAny();
      },
      label: '删除',
      type: ContextMenuButtonType.custom,
    ));
    results.add(ContextMenuButtonItem(
      onPressed: () {
        print("collect!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        ContextMenuController.removeAny();
      },
      label: '多选',
      type: ContextMenuButtonType.custom,
    ));
    results.add(ContextMenuButtonItem(
      onPressed: () {
        print("collect!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        ContextMenuController.removeAny();
      },
      label: '引用',
      type: ContextMenuButtonType.custom,
    ));

    return results;
  }

  @override
  void dispose() {
    super.dispose();
    _textEditingController.dispose();
    _focusNode.dispose();
  }

}
